{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "112b7db7",
   "metadata": {},
   "source": [
    "### 课本第4页代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "df354f76",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This minute seems a little odd.\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "odds = [ 1, 3, 5, 7, 9,11,13,15,17,19,\n",
    "        21,23,25,27,29,31,33,35,37,39,\n",
    "        41,43,45,47,49,51,53,55,57,59 ]\n",
    "right_this_minute = datetime.today().minute\n",
    "\n",
    "if right_this_minute in odds:\n",
    "    print(\"This minute seems a little odd.\")\n",
    "else:\n",
    "    print(\"Not an odd minute\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ea543da",
   "metadata": {},
   "source": [
    "### 函数+模块=标准库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "94156e9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2021, 11, 17)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import datetime\n",
    "datetime.date.today()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7a484b57",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.date.today().day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0a2e1e66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.date.today().month"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "64468773",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2021"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.date.today().year"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e734f97b",
   "metadata": {},
   "source": [
    "#### 调用date.isoformat 函数并传入今天的日期"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "76e3eaa4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021-11-17'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.date.isoformat(datetime.date.today())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "da835c61",
   "metadata": {},
   "outputs": [],
   "source": [
    "#### 导入time模块后，调用strftime 函数并显示时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "0b147582",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'20:56'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "time.strftime(\"%H:%M\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e31d173",
   "metadata": {},
   "source": [
    "#### 想知道周几？是上午还是下午？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "683e6f01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Wednesday PM'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "time.strftime(\"%A %p\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b296ce31",
   "metadata": {},
   "source": [
    "### 对应\"if\"可以有什么\"else\"?\n",
    "#### 例子:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be411623",
   "metadata": {},
   "source": [
    "if today == 'Saturday':\n",
    "    print('Party!!')\n",
    "elif today =='Sunday':\n",
    "    print('Recover.')\n",
    "else:\n",
    "    print('Work,work,work.')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f45ebc9",
   "metadata": {},
   "source": [
    "### for 循环\n",
    "#### 例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "6564a55a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "for i in [1,2,3]:\n",
    "      print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "873cab15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H\n",
      "i\n",
      "!\n"
     ]
    }
   ],
   "source": [
    "for ch in \"Hi!\":\n",
    "    print(ch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "e4450d3c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Head First Rocks!\n",
      "Head First Rocks!\n",
      "Head First Rocks!\n",
      "Head First Rocks!\n",
      "Head First Rocks!\n"
     ]
    }
   ],
   "source": [
    "for num in range(5):\n",
    "    print('Head First Rocks!')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ad9e889",
   "metadata": {},
   "source": [
    "#### 测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1d903e89",
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "import random\n",
    "import time\n",
    "\n",
    "odds = [ 1, 3, 5, 7, 9,11,13,15,17,19,\n",
    "        21,23,25,27,29,31,33,35,37,39,\n",
    "        41,43,45,47,49,51,53,55,57,59 ]\n",
    "for i in range(5):\n",
    "    right_this_minute = datetime.today().minute\n",
    "    if right_this_minute in odds:\n",
    "        print(\"This minute seems a little odd.\")\n",
    "    else:\n",
    "        print(\"Not an odd minute\")\n",
    "    wait_time = random.randint(1,60)\n",
    "    time.sleep(wait_time)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "968b67c3",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53ddbaf9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb82d4dc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5f3f921",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This minute seems a little odd.\n",
      "Not an odd minute\n",
      "Not an odd minute\n",
      "Not an odd minute\n",
      "Not an odd minute\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4dcfbaac",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
